package com.andy.blog.quartz.controller;

import com.andy.blog.common.annotation.Log;
import com.andy.blog.common.enums.BusinessTypeEnum;
import com.andy.blog.common.page.PageUtilsDTO;
import com.andy.blog.common.reponse.GeneralResponseResult;
import com.andy.blog.common.utils.EasyExcelUtils;
import com.andy.blog.dal.vo.SysJobLogVO;
import com.andy.blog.quartz.dto.SysJobLogDTO;
import com.andy.blog.quartz.service.SysJobLogBusService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 * Created by andy on 2023-04-05 11:38
 */
@RestController
@RequestMapping("/monitor/jobLog")
public class SysJobLogController {

    @Resource
    private SysJobLogBusService sysJobLogBusService;

    /**
     * 查询定时任务调度日志列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:list')")
    @GetMapping("/list")
    public GeneralResponseResult<PageUtilsDTO<SysJobLogDTO>> list(SysJobLogVO sysJobLogVO) {
        return GeneralResponseResult.success(sysJobLogBusService.selectListByPage(sysJobLogVO));
    }

    /**
     * 导出定时任务调度日志列表
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:export')")
    @Log(title = "任务调度日志", businessType = BusinessTypeEnum.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysJobLogVO sysJobLogVO) {
        List<SysJobLogDTO> list = sysJobLogBusService.selectListByCondition(sysJobLogVO);
        EasyExcelUtils.writeStyleExcel(response, list, "调度日志", SysJobLogDTO.class);
    }

    /**
     * 根据调度编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:query')")
    @GetMapping(value = "/{jobLogId}")
    public GeneralResponseResult<SysJobLogDTO> getInfo(@PathVariable Long jobLogId) {
        return GeneralResponseResult.success(sysJobLogBusService.selectJobLogById(jobLogId));
    }

    /**
     * 删除定时任务调度日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "定时任务调度日志", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{jobLogIds}")
    public GeneralResponseResult<Void> remove(@PathVariable List<Long> jobLogIds) {
        sysJobLogBusService.deleteJobLogByIds(jobLogIds);
        return GeneralResponseResult.success();
    }

    /**
     * 清空定时任务调度日志
     */
    @PreAuthorize("@ss.hasPermi('monitor:job:remove')")
    @Log(title = "调度日志", businessType = BusinessTypeEnum.CLEAN)
    @DeleteMapping("/clean")
    public GeneralResponseResult<Void> clean() {
        sysJobLogBusService.cleanJobLog();
        return GeneralResponseResult.success();
    }
}
